import {
    showToast,
    switchTab
} from "../../utils/asyncWx.js";
import { insert } from "../../utils/cloud.js";
var app =  getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        content: '',
        time: "",
        //背景图片和输入颜色
        imageurl: '../../icons/common_bj.png',
        input_color:'white',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (e) {
        this.setData({
            time: Date.now()
        });
    },

    /**
     * 生命周期回调——监听小程序启动或切前台。
     */
    onShow: function () {
        this.setData({
            time: Date.now(),
            content: "",
            imageurl: app.data.type == 'common' ? '../../icons/common_bj.png' : '../../icons/mood.png',
            input_color: app.data.type == 'common' ?'white':'blue',
        });
    },

    /**
     * input change事件
     */
    change (e) {
        var { value } = e.detail;
        this.setData({
            content: value
        });
    },

    /**
     * cancel 事件
     */
    cancel () {
        //类型重置
        app.data.type = "common";
        this.setData({
            content: '',
            imageurl: '../../icons/common_bj.png',
            input_color:'white',
        });
        switchTab({
            url: "/pages/list/list",
        });
        
    },

    sure () {
        var reg = /^\s*$/g;
        if (!this.data.content || reg.test(this.data.content)) {
            showToast({
                title: "内容不能为空",
                icon: error,
            });
            return;
        }
        this.setData({
            time: Date.now(),
        });
        setValue(this);
        
        showToast({ title: "跳转中", icon: 'loading' })
            .then(res => {
                var that = this;
                setTimeout(function () {
                    //存完清空输入框的内容
                    that.setData({
                        content: '',
                        imageurl: '../../icons/common_bj.png',
                        input_color:'white',
                    });
                    // 类型重置
                    app.data.type = "common";
                    switchTab({
                        url: "../list/list"
                    });
                }, 1000);
                console.log(res);
            }).catch(err => {
                console.log(err);
            });
    }
});

/**
 * 设置填写的信息，新增
 */
 async function setValue(page) {
     var diary = wx.getStorageSync('diary') || [];
     var data = [];
     //是否为最新建
     var flag = (diary.length != 0);
    //  console.log(flag);
    if(flag) {
        data = ({
            //创建时间
            time: Date.now(),
            //内容
            content: page.data.content,
            //类型
            type: app.data.type,
        });
    }else {
        data = ({
            //创建时间
            time: page.data.time,
            //内容
            content: page.data.content,
            //类型
            type: app.data.type,
        });
     }
    //  console.log(data);
    //  console.log(data[data.length-1]);
        //添加数据到云端
     const { _id } = await insert({
         table: "diary",
         data: data,
     });
     diary.push({
         time: data.time,
         content: data.content,
         _id: _id,
         type: app.data.type,
     });
      //将日记保存到本地里
     wx.setStorageSync('diary', diary);
  }